package composite;

/**
 * @ClassName CompositePatternDemo
 * @Description 当同一个类型的对象有树形关系的时候, 使用组合模式, 可以使得操作一个对象极其子对象非常简单.
 * 例如: 部门->子部门->子部门
 * @Author leejiliang
 * @DateTime 2021/7/20 16:32
 * @Version 1.0
 */
public class CompositePatternDemo {

	public static void main(String[] args) {
		final Department department1 = new Department(1, "1");
		final Department department2 = new Department(2, "1-2");
		final Department department3 = new Department(3, "1-3");
		final Department department4 = new Department(4, "2-1");
		final Department department5 = new Department(5, "2-2");
		final Department department6 = new Department(6, "2-3");

		department1.getSubDepartments().add(department2);
		department1.getSubDepartments().add(department3);
		department2.getSubDepartments().add(department4);
		department2.getSubDepartments().add(department5);
		department2.getSubDepartments().add(department6);

		// 递归打印department1极其子部门
		print(department1);
	}

	public static void print(Department department) {

		System.out.println(department.toString());

		if (department.getSubDepartments().size() <= 0) {
			return;
		} else {
			department.getSubDepartments().forEach(i -> print(i));
		}

	}
}
